package com.lqg.bookLibrary.service.webService;

import com.lqg.bookLibrary.common.core.domain.JsonResult;
import com.lqg.bookLibrary.common.exception.ServiceException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.*;
import java.nio.charset.StandardCharsets;

@Component
public class FileService {
    private static final Logger logger = LoggerFactory.getLogger(FileService.class);

    /**
     * 文件上传服务
     * @param zipFile 上传文件流
     * @param path 存储在本服务器的路径
     * @param fileName 文件名称
     * */
    public void upLoadFile(MultipartFile zipFile, String path, String fileName) {
        File targetFile = new File(path,fileName);
        try {
            FileUtils.forceMkdir(new File(path));
        } catch (IOException e) {
            e.printStackTrace();
            throw new ServiceException("创建文件夹:"+path+"失败");
        }
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(targetFile);
            IOUtils.copy(zipFile.getInputStream(),fileOutputStream);
            logger.info("------>>>>>>成功上传一个文件<<<<<<------");
        } catch (IOException e) {
            e.printStackTrace();
            throw new ServiceException("IO读写错误");
        }finally {
            if(fileOutputStream!=null){
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    logger.error("输出流关闭错误",e);
                }
            }
        }
    }

    /**
     * 读取指定URI的文章字符内容
     * */
    public String readURIContest(String uri){
        String context=null;
        try {
            context = IOUtils.toString(URI.create(uri), StandardCharsets.UTF_8);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException(uri+"是错误的文件地址");
        }
        return context;
    }

    public String readContest(String uri){
        String con=null;
        try {
            con = readURIContest(uri);
        } catch (Exception e) {
            e.printStackTrace();
            return e.getMessage();
        }
        return con;
    }

    // 把字符串写入文件
    public void saveStringToFile(String content,String path,String filename){
        try {
            File file = new File(path,filename);
            FileUtils.writeStringToFile(file, content, "UTF-8");
        } catch (IOException e) {
            e.printStackTrace();
            throw new ServiceException("IO异常");
        }
    }


    // 把字符串写入文件
    public void copyURLToFile(String url, String path, String filename){
        try {
            //Proxy proxy = new Proxy(Proxy.Type.HTTP, new
            //        InetSocketAddress("175.4.130.211", 26387));
            URL url1 = new URL(url);
            //URLConnection urlConnection = url1.openConnection(proxy);
            URLConnection urlConnection = url1.openConnection();
            urlConnection.addRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36");
            InputStream stream = urlConnection.getInputStream();
            File file = new File(path,filename);
            Throwable var3 = null;
            try {
                FileUtils.copyInputStreamToFile(stream, file);
            } catch (Throwable var12) {
                var3 = var12;
                throw var12;
            } finally {
                if (stream != null) {
                    if (var3 != null) {
                        try {
                            stream.close();
                        } catch (Throwable var11) {
                            var3.addSuppressed(var11);
                        }
                    } else {
                        stream.close();
                    }
                }

            }
        } catch (IOException e) {
            e.printStackTrace();
            throw new ServiceException("IO异常");
        }
    }

}
